package com.rupeng.web.controller;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.rupeng.pojo.Card;
import com.rupeng.pojo.Chapter;
import com.rupeng.pojo.Question;
import com.rupeng.pojo.Segment;
import com.rupeng.pojo.User;
import com.rupeng.pojo.UserCard;
import com.rupeng.pojo.UserSegment;
import com.rupeng.service.CardService;
import com.rupeng.service.ChapterService;
import com.rupeng.service.QuestionService;
import com.rupeng.service.SegmentService;
import com.rupeng.service.UserCardService;
import com.rupeng.service.UserSegmentService;
import com.rupeng.util.AjaxResult;
import com.rupeng.util.CommonUtils;

@Controller
@RequestMapping("/segment")
public class SegmentController {
	@Autowired
	SegmentService segmentService;
	@Autowired
	ChapterService chapterService;
	@Autowired
	UserCardService userCardService;
	@Autowired
	UserSegmentService userSegmentService;
	@Autowired
	CardService cardService;
	@Autowired
	QuestionService questionService;
	@RequestMapping("/detail.do")
	public ModelAndView detail(Long id, HttpServletRequest request) {
		Segment segment = segmentService.selectOne(id);
		if (segment==null) {
			return new ModelAndView("404");
		}
		Chapter chapter = chapterService.selectOne(segment.getChapterId());
		User user = (User) request.getSession().getAttribute("user");
		UserCard userCard = new UserCard();
		userCard.setUserId(user.getId());
		userCard.setCardId(chapter.getCardId());
		userCard = userCardService.selectOne(userCard);
		if (userCard==null) {
			return new ModelAndView("message", "message", "用户不拥有此学习卡");
		} else if (userCard.getEndTime().getTime()<System.currentTimeMillis()) {
			return new ModelAndView("message", "message", "这张学习卡已经过期");
		}
		ModelAndView modelAndView = new ModelAndView("segment/detail");
        modelAndView.addObject("segment", segment);
        modelAndView.addObject("chapter", chapter);
        Card card = cardService.selectOne(chapter.getCardId());
        modelAndView.addObject("card", card);
        Question question = new Question();
        question.setSegmentId(id);
        question.setIsResolved(true);
        List<Question> questions = questionService.selectList(question, "resolvedTime desc");
        modelAndView.addObject("questions", questions);
        //添加学习记录
        UserSegment userSegment = new UserSegment();
        userSegment.setCreateTime(new Date());
        userSegment.setSegmentId(segment.getId());
        userSegment.setUserId(user.getId());
        userSegmentService.insert(userSegment);
        return modelAndView;
	}
	@RequestMapping("/next.do")
	public ModelAndView next(Long id, HttpServletRequest request) {
		Segment segment = segmentService.selectOne(id);
		if (segment==null) {
			return new ModelAndView("404");
		}
		long chapterId = segment.getChapterId();
		int segmentSeqNum = segment.getSeqNum();
		segment = new Segment();
		segment.setChapterId(chapterId);
		List<Segment> segments = segmentService.selectList(segment, "seqNum asc");
		if (segments.get(segments.size()-1).getSeqNum()==segmentSeqNum) {
			Chapter chapter = chapterService.selectOne(chapterId);
			int chapterSeqNum = chapter.getSeqNum();
			long cardId = chapter.getCardId();
			chapter = new Chapter();
			chapter.setCardId(cardId);
			List<Chapter> chapters = chapterService.selectList(chapter, "seqNum asc");
			if (chapters.get(chapters.size()-1).getSeqNum()==chapterSeqNum) {
				return new ModelAndView("message", "message", "此学习卡已经没有下节课程了");
			}
			for (int i=0; i<chapters.size(); i++) {
				chapter = chapters.get(i);
				if (chapter.getSeqNum()>chapterSeqNum) {
					segment.setChapterId(chapter.getId());
					segments = segmentService.selectList(segment, "seqNum asc");
					if (!CommonUtils.isEmpty(segments)) {
						return detail(segments.get(0).getId(), request);
					}
				}
			}
			return new ModelAndView("message", "message", "此学习卡已经没有下节课程了");
		}
		for (int i=0; i<segments.size(); i++) {
			segment = segments.get(i);
			if (segment.getSeqNum()>segmentSeqNum) {
				break;
			}
		}
		return detail(segment.getId(), request);
	}
	@RequestMapping("/list.do")
    public @ResponseBody AjaxResult list(Long chapterId) {
		if (chapterId == null) {
			throw new RuntimeException("篇章id不能为空");
        }
		Segment segment = new Segment();
		segment.setChapterId(chapterId);
        List<Segment> segments = segmentService.selectList(segment, "seqNum asc");
        return AjaxResult.successInstance(segments);
    }
}
